package com.funsport.controller.mobile;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funsport.common.Result;
import com.funsport.entity.Notification;
import com.funsport.service.NotificationService;
import com.funsport.security.JwtTokenProvider;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 移动端通知Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/mobile/notifications")
@RequiredArgsConstructor
@Tag(name = "移动端通知")
public class MobileNotificationController {
    
    private final NotificationService notificationService;
    private final JwtTokenProvider jwtTokenProvider;
    
    @Operation(summary = "获取通知列表")
    @GetMapping("")
    public Result<Page<Notification>> getNotifications(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Boolean isRead,
            HttpServletRequest request
    ) {
        try {
            String token = request.getHeader("Authorization");
            if (token != null && token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            Long userId = token != null ? jwtTokenProvider.getUserIdFromToken(token) : null;
            if (userId == null) {
                return Result.error("请先登录");
            }
            
            Page<Notification> result = notificationService.getUserNotifications(userId, page, pageSize, isRead);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取通知列表失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "标记为已读")
    @PostMapping("/{id}/read")
    public Result<Void> markAsRead(
            @PathVariable Long id,
            HttpServletRequest request
    ) {
        try {
            String token = request.getHeader("Authorization");
            if (token != null && token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            Long userId = token != null ? jwtTokenProvider.getUserIdFromToken(token) : null;
            if (userId == null) {
                return Result.error("请先登录");
            }
            
            notificationService.markAsRead(id, userId);
            return Result.success();
        } catch (Exception e) {
            log.error("标记已读失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
    
    @Operation(summary = "标记全部已读")
    @PostMapping("/read-all")
    public Result<Void> markAllAsRead(HttpServletRequest request) {
        try {
            String token = request.getHeader("Authorization");
            if (token != null && token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            Long userId = token != null ? jwtTokenProvider.getUserIdFromToken(token) : null;
            if (userId == null) {
                return Result.error("请先登录");
            }
            
            notificationService.markAllAsRead(userId);
            return Result.success();
        } catch (Exception e) {
            log.error("标记全部已读失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
}
